from flask import jsonify
from ..exceptions import ValidationError
from . import api

# 无效请求
def bad_request(message):
    response = jsonify({'error': 'bad request', 'message': message})
    response.status_code = 400
    return response

# 未授权请求
def unauthorized(message):
    response = jsonify({'error': 'unauthorized', 'message': message})
    response.status_code = 400
    return response

# 禁止访问
def forbidden(message):
    response = jsonify({'error': 'forbidden', 'message': message})
    response.status_code = 403
    return response

# 未实现请求，服务器内部错误
def fail(message):
    response = jsonify({'error': 'fail', 'message': message})
    response.status_code = 501
    return response

@api.errorhandler(ValidationError)
def vaildation_error(e):
    return bad_request(e.args[0])